﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kassasysteem_TacoTruijens
{
    public partial class reservations : System.Web.UI.Page
    {
        private DataClassesDataContext Database = new DataClassesDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            addHeaderRowToTableReservations();
            fillTableReservations();
        }

        private void fillTableReservations()
        {
            var reservations = (from r in Database.Reserverings
                               orderby r.nummer descending
                               select r).Take(30);

            String reservationNumber = Request.QueryString["reservationnumber"];

            if (reservationNumber != null)
            {
                reservations = null;
                reservations = from r in Database.Reserverings
                               where r.nummer == Convert.ToInt32(reservationNumber)
                               orderby r.nummer descending
                               select r;

                labelSearch.Text = "U heeft gezocht op reserveringsnummer " + reservationNumber + ".";
            }
            
            Boolean even = false;

            foreach (Reservering r in reservations)
            {
                var tickets = from t in Database.Tickets
                              where t.Reservering == r
                              select t;

                if (tickets.Count() != 0)
                {
                    Ticket ticket = tickets.First();

                    TableRow showTableRow = new TableRow();

                    if (even)
                    {
                        showTableRow.CssClass = "even";
                        even = false;
                    }
                    else
                    {
                        even = true;
                    }

                    TableCell numberTableCell = new TableCell();
                    TableCell showTableCell = new TableCell();
                    TableCell spacesTableCell = new TableCell();
                    TableCell confirmTableCell = new TableCell();
                    TableCell cancelTableCell = new TableCell();

                    confirmTableCell.CssClass = "alignCenter";
                    cancelTableCell.CssClass = "alignCenter";

                    numberTableCell.Text = r.nummer.ToString();
                    showTableCell.Text = ticket.Voorstelling.datumtijd.ToString() + ": " + ticket.Voorstelling.Film.naam;
                    spacesTableCell.Text = getSpaces(r).ToString();
                    confirmTableCell.Text = "<a href='payreservation.aspx?reservationnumber=" + r.nummer + "'><img src='images/accept.gif' alt='Bevestig' title='Bevestig reservering' /></a>";
                    cancelTableCell.Text = "<a href='cancelreservation.aspx?reservationnumber=" + r.nummer + "'><img src='images/delete.png' alt='Annuleer' title='Annuleer reservering' /></a>";

                    showTableRow.Cells.Add(numberTableCell);
                    showTableRow.Cells.Add(showTableCell);
                    showTableRow.Cells.Add(spacesTableCell);
                    showTableRow.Cells.Add(confirmTableCell);
                    showTableRow.Cells.Add(cancelTableCell);

                    tableReservations.Rows.Add(showTableRow);
                }
            }
        }

        private int getSpaces(Reservering r)
        {
            int returnvalue = 0;

            var tickets = from t in Database.Tickets
                          where t.Reservering == r
                          select t;

            returnvalue = tickets.Count();

            return returnvalue;
        }

        private void addHeaderRowToTableReservations()
        {
            TableRow headerTableRow = new TableRow();
            headerTableRow.CssClass = "header";

            TableCell dateTimeTableCell = new TableCell();
            TableCell showTableCell = new TableCell();
            TableCell spacesTableCell = new TableCell();
            TableCell confirmTableCell = new TableCell();
            TableCell cancelTableCell = new TableCell();

            dateTimeTableCell.Text = "Reserveringsnummer";
            showTableCell.Text = "Voorstelling";
            spacesTableCell.Text = "Aantal plaatsen";
            confirmTableCell.Text = "Bevestig";
            cancelTableCell.Text = "Annuleer";

            headerTableRow.Cells.Add(dateTimeTableCell);
            headerTableRow.Cells.Add(showTableCell);
            headerTableRow.Cells.Add(spacesTableCell);
            headerTableRow.Cells.Add(confirmTableCell);
            headerTableRow.Cells.Add(cancelTableCell);

            tableReservations.Rows.Add(headerTableRow);
        }

        private void deleteReservations()
        {
            var reservations = from r in Database.Reserverings
                               select r;

            foreach (Reservering r in reservations)
            {
                var tickets = from t in Database.Tickets
                              where t.Reservering == r
                              where t.voorstelling_datumtijd < DateTime.Now.AddMinutes(30)
                              select t;

                if (tickets.Count() > 0)
                {
                    foreach (Ticket t in tickets)
                    {
                        Database.Tickets.DeleteOnSubmit(t);
                    }

                    Database.Reserverings.DeleteOnSubmit(r);
                }           
            }

            Database.SubmitChanges();
        }

        protected void buttonSearch_Click1(object sender, EventArgs e)
        {
            String reservationNumber = textBoxNumber.Text;

            if (reservationNumber.Length > 0)
            {
                String queryString = "reservations.aspx?reservationnumber=" + reservationNumber;
                Server.Transfer(queryString);
            }
        }

        protected void buttonRemoveAll_Click(object sender, EventArgs e)
        {
            deleteReservations();
            Server.Transfer("deletereservations.aspx");
        }
    }
}